﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace VIB.View
{
    /// <summary>
    /// Interaction logic for PromptUserId.xaml
    /// </summary>
    public partial class GetUserId : Window
    {
        public int UserId {get;set;}

        public GetUserId()
        {
            InitializeComponent();
        }

        private void RefreshControlStatus(object sender, TextChangedEventArgs e)
        {
            if(textbox_iduser.Text != String.Empty && Regex.IsMatch(textbox_iduser.Text, @"[\d]"))
            {
                button_ok.IsEnabled = true;
            }
            else if (textbox_iduser.Text != String.Empty && !Regex.IsMatch(textbox_iduser.Text, @"[\d]"))
            {
                button_ok.IsEnabled = false;
                textblock_error.Text = "Ungültige Eingabe";
            }
            else
            {
                button_ok.IsEnabled = false;
                textblock_error.Text = "Bitte Personalnummer eingeben";
            }
        }

        private void button_ok_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int temp = -1;
                Int32.TryParse(textbox_iduser.Text, out temp);

                if (temp > 0 && DataManager.IsUserIdValid(temp))
                {
                    UserId = temp;
                    this.DialogResult = true;
                    this.Close();
                }
                else
                {
                    textblock_error.Text = "Keine gültige Personalnummer";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
        }

        private void button_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void textbox_iduser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button_ok_Click(sender, e);
            }
        }
    }
}
